from fastapi import FastAPI
from app.api import api_router
from app.config import DESCRIPTION, TITLE, BASE_DIR

from app.libs.exceptions import (
    forbidden_error_exception,
    not_found_error_exception,
    server_error_exception
)


def create_app():
    app = FastAPI(title=TITLE, description=DESCRIPTION)

    app.mount(
        "/static",
        StaticFiles(directory=os.path.join(BASE_DIR, "static")),
        name="static",
    )

    # 加入模块路由  类似 flask bp
    app.include_router(api_router)

    # 设置跨域
    app.add_middleware(
        CORSMiddleware,
        allow_origins=setting.ORIGINS,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    return app





# from app.config import SQLALCHEMY_DATABASE_URI
# from sqlalchemy.orm import sessionmaker
# from sqlalchemy import create_engine

# engine = create_engine(
#         SQLALCHEMY_DATABASE_URI,
#         max_overflow=0,  # 超过连接池大小外最多创建的连接
#         pool_size=5,  # 连接池大小
#         pool_timeout=30,  # 池中没有线程最多等待的时间，否则报错
#         pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收（重置）
#     )

# # 数据库会话
# def create_session():
#     return sessionmaker(autocommit=False, autoflush=False, bind=engine)
# session = create_session()

